<?php

namespace app\common\model\xiservice;

use think\Model;
use traits\model\SoftDelete;

class Service extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiservice_service';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'image_text',
        'images_arr',
        'nums',
        'bargain_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getBargainList()
    {
        return ['0' => __('Bargain 0'), '1' => __('Bargain 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getChoseList()
    {
        return ['0' => __('Chose 0'), '1' => __('Chose 1')];
    }

     public function getNumsAttr($value, $data)
    {
        $basicnums = isset($data['basicnums']) ? $data['basicnums'] : 0;
        $salenums = isset($data['salenums']) ? $data['salenums'] : 0;
        $all = $basicnums+$salenums;
        return $all;
    }
   

    public function getBargainTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bargain']) ? $data['bargain'] : '');
        $list = $this->getBargainList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getImageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');
        return cdnurl($value,true);      
    }

    public function servicecategory()
    {
        return $this->belongsTo('app\common\model\xiservice\ServiceCategory', 'service_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function getImagesArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $array = [];
        if($value){
            $list = explode(',', $value);
            for($i=0;$i<count($list);$i++){
                array_push($array, cdnurl($list[$i],true));
            }
        }
        return $array;      
    }
}
